package de.oszimtcc.timetablehelper;



import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;
import de.oszimtcc.framework.BaseActivity;
import de.oszimtcc.timetable.TimetableApplication;
import de.oszimtcc.timetablescreens.TimetableScreen;

public class ViewFlipperGestureDetector extends SimpleOnGestureListener
{
	private ViewConfiguration vc;
	private int SWIPE_MIN_DISTANCE = 20;
	private int SWIPE_MAX_OFF_PATH = 100;
	//private static final int SWIPE_MIN_DISTANCE = 120;
	//private static final int SWIPE_MAX_OFF_PATH = 250;
	  
	private int SWIPE_THRESHOLD_VELOCITY = 200;
	
	ViewFlipper flipper;
	BaseActivity c;
	
	public ViewFlipperGestureDetector(BaseActivity c, ViewFlipper flipper) 
	{
		super();
		this.flipper = flipper;
		this.c = c;
		vc = ViewConfiguration.get(TimetableApplication.getInstance().getContext());
		SWIPE_MIN_DISTANCE = vc.getScaledTouchSlop();
		SWIPE_THRESHOLD_VELOCITY = vc.getScaledMinimumFlingVelocity();
	}
	
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
	{
		if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
			return false;
		if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
		{
			flipper.setInAnimation(ViewFlipperGestureDetector.inFromRightAnimation());
			flipper.setOutAnimation(ViewFlipperGestureDetector.outToLeftAnimation());
			flipper.showNext();
			((TimetableScreen)this.c.screen).UpdateDayBar();
		} 
		else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
		{
			flipper.setInAnimation(ViewFlipperGestureDetector.inFromLeftAnimation());
			flipper.setOutAnimation(ViewFlipperGestureDetector.outToRightAnimation());
			flipper.showPrevious();
			((TimetableScreen)this.c.screen).UpdateDayBar();
		}
		return super.onFling(e1, e2, velocityX, velocityY);
	}
	
	
	@Override
	public boolean onDown (MotionEvent e) 
	{
		super.onDown(e);
		return true;
	}
			
	public static Animation inFromRightAnimation() 
	{
		Animation inFromRight = new TranslateAnimation(
		Animation.RELATIVE_TO_PARENT, +1.0f,
		Animation.RELATIVE_TO_PARENT, 0.0f,
		Animation.RELATIVE_TO_PARENT, 0.0f,
		Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromRight.setDuration(500);
		inFromRight.setInterpolator(new AccelerateInterpolator());
		return inFromRight;
	}
	
	public static Animation outToLeftAnimation() 
	{
		Animation outtoLeft = new TranslateAnimation(
		Animation.RELATIVE_TO_PARENT, 0.0f,
		Animation.RELATIVE_TO_PARENT, -1.0f,
		Animation.RELATIVE_TO_PARENT, 0.0f,
		Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoLeft.setDuration(500);
		outtoLeft.setInterpolator(new AccelerateInterpolator());
		return outtoLeft;
	}
	
	public static Animation inFromLeftAnimation() 
	{
		Animation inFromLeft = new TranslateAnimation(
		Animation.RELATIVE_TO_PARENT, -1.0f,
		Animation.RELATIVE_TO_PARENT, 0.0f,
		Animation.RELATIVE_TO_PARENT, 0.0f,
		Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromLeft.setDuration(500);
		inFromLeft.setInterpolator(new AccelerateInterpolator());
		return inFromLeft;
	}
	
	public static Animation outToRightAnimation() 
	{
		Animation outtoRight = new TranslateAnimation(
		Animation.RELATIVE_TO_PARENT, 0.0f,
		Animation.RELATIVE_TO_PARENT, +1.0f,
		Animation.RELATIVE_TO_PARENT, 0.0f,
		Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoRight.setDuration(500);
		outtoRight.setInterpolator(new AccelerateInterpolator());
		return outtoRight;
	}
}